package com.rpj.stauy.config;

import com.rpj.stauy.service.PersistenceChatAssistent;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
public class PersistenceConfig {

    @Resource
    private RedisChatMemoryStore redisChatMemoryStore;

    @Bean("chat")
    public ChatModel chatModel(){
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("aliQWen_api"))
                .modelName("qwen-long")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean("redischat")
    public PersistenceChatAssistent persistenceChatAssistent(){
        ChatMemoryProvider chatMemoryProvider = memoryId ->
            MessageWindowChatMemory.builder()
                    .id(memoryId)
                    .maxMessages(1000)
                    .chatMemoryStore(redisChatMemoryStore)
                    .build();

        return AiServices.builder(PersistenceChatAssistent.class)
                .chatMemoryProvider(chatMemoryProvider)
                .chatModel(chatModel())
                .build();
    }
}
